﻿using System;
using System.Collections.Generic;
using System.Collections.Generic.Sync;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace Tcp2Http_ConsoleApplication
{
    class Program
    {

        static SyncDictionary<TcpMappingItem, Guid> pool = new SyncDictionary<TcpClient, Guid>();

        static void Main(string[] args)
        {
            TcpListener tl = new TcpListener(3389);
            tl.Start();
            while (true)
            {

                var tcp = tl.AcceptTcpClient();

         
                TcpMappingItem ti = new TcpMappingItem();
                ti.TcpStream = tcp.GetStream();
                ti.Received += new EventHandler(ti_Received);
                pool.Add(ti, Guid.NewGuid());
            }

        }

        static void ti_Received(object sender, EventArgs e)
        {
            var s = (TcpMappingItem)sender;
            var svc = new Http2TcpService.Http2TcpServiceClient();

            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            byte[] bts;
            while (s.ReceiveBytes(out bts))
            {
                ms.Write(bts, 0, bts.Length); 
            }

            svc.Send("127.0.0.1", 3389, ,pool[s]);

        }



        static void receive(IAsyncResult ia)
        { 
        
        
        }


    }




}
